﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ExhibitionDraw.Common;
using ExhibitionDraw.Model;

namespace ExhibitionDraw.Control
{
    public partial class ColorChild : ChildWindow
    {
        Service.CommServiceClient client = Server.ServiceHelper.InitService();
        List<Service.ColorTypeModel> ret = new List<Service.ColorTypeModel>();
        public ColorChild()
        {
            InitializeComponent();
          
        }

        private void InitData()
        {

            client.GetColorListAsync();
            client.GetColorListCompleted += new EventHandler<Service.GetColorListCompletedEventArgs>(GetColorListComplete); 
        }

        private void GetColorListComplete(object sender, Service.GetColorListCompletedEventArgs e)
        {
            
            Service.ColorTypeModel ctl = new Service.ColorTypeModel();
            ctl.ColorTypeName="新增";
            ret.Add(ctl);
            if (e.Result.Count() > 0) {
                ret.AddRange(e.Result);
                //e.Result.Insert(0, ctl);
                CbxColor.DisplayMemberPath = "ColorTypeName";
                CbxColor.ItemsSource = ret;
                CbxColor.SelectedIndex = 0;
              
            }
           // CbxColor.Items.Add("新增");
            //CbxColor.Items.Insert(0,lbi);

            //this.CbxColor.SelectedItem = lbi;


            
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            Service.ColorTypeModel tpl = new Service.ColorTypeModel();
            if (!string.IsNullOrEmpty(hfId.Text)) {
                tpl.Id =Convert.ToInt32( hfId.Text); 
            }
            tpl.ColorTypeName = ColorTypeName.Text;
            tpl.FColor = FontColor.Text;
            tpl.CanOrderColor = OrderStatus.Text;
            tpl.OrderColor = UnOrderStatus.Text;
            tpl.PayColor = PayStatus.Text;
            tpl.OtherColor = OtherStatus.Text;

            client.SaveColorModelAsync(tpl);
            client.SaveColorModelCompleted += new EventHandler<Service.SaveColorModelCompletedEventArgs>(color_SaveCompleted);
        }


        public void color_SaveCompleted(object sender,Service.SaveColorModelCompletedEventArgs e ) {
            string strMsg = string.Empty;
            if (e.Result > 0)
            {
                strMsg = "数据保存成功！";
                this.DialogResult = false;
            }
            else {
                strMsg = "数据保存失败！";
            }

            MessageWin win = new MessageWin();
            win.txtMsg.Text = strMsg;
            win.Show();
           
            
        
        }
        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close ();
        } 

        private void btnFColor_Click(object sender, RoutedEventArgs e)
        {
            //colorPicker.Visibility = Visibility.Visible;
        }

        private void ColorPicker_ColorChanged(object sender, ColorChangedEventArgs e)
        {
            //FontColor.Text = colorPicker.CompactHexText.Text;
        }

        private void CbxColor_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Service.ColorTypeModel colorType = (sender as ComboBox).SelectedItem as Service.ColorTypeModel;
            if (colorType != null && colorType.ColorTypeName != "新增")
            {
                hfId.Text = colorType.Id.ToString();
                ColorTypeName.Text = colorType.ColorTypeName;
                FontColor.Text = colorType.FColor;

                btnFColor.Background = new SolidColorBrush(CommFun.ConvertToColor(colorType.FColor));
                OrderStatus.Text = colorType.CanOrderColor;
                
                UnOrderStatus.Text = colorType.OrderColor;
                PayStatus.Text = colorType.PayColor;
                OtherStatus.Text = colorType.OtherColor;
            }
            else {
                hfId.Text = "0";
                ColorTypeName.Text = "";
                FontColor.Text = "";
                OrderStatus.Text = "";
                UnOrderStatus.Text = "";
                PayStatus.Text = "";
                OtherStatus.Text = "";
            }

        }

        private void btnFColor_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //colorPicker.Visibility = Visibility.Visible;
            ColorPicker clp = new ColorPicker();
           // clp.SetValue(Canvas.TopProperty, GetValue(FontColor.));
        }

       
    }
}

